package face.pay.profit.impl;

import com.alibaba.fastjson.JSON;
import face.pay.constant.OrderType;
import face.pay.constant.ProfitStrategy;
import face.pay.dao.*;
import face.pay.entity.*;
import face.pay.profit.IprofitService;
import face.pay.profit.UserProfitBiz;
import face.pay.profit.core.RelationInfo;
import face.pay.profit.core.RelationType;
import face.pay.profit.strategy.DefaultProfitStrategy;
import face.pay.profit.strategy.PercentageProfitStrategy;
import face.pay.server.IOemProfitConfigServer;
import face.pay.util.MathUtil;
import face.pay.vo.ProfitUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/8/5 14:57
 * @Description:
 */
@Service("profitCacheService")
@Slf4j
public class ProfitCacheServiceIml implements IprofitService {

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    IncomeInfoDao incomeInfoDao;

    @Autowired
    EnterpriseInfoDao enterpriseInfoDao;

    @Autowired
    UserInfoDao userInfoDao;

    @Autowired
    DefaultProfitStrategy defaultProfitStrategy;

    @Autowired
    PercentageProfitStrategy percentageProfitStrategy;


    @Autowired
    UserProfitBiz userProfitBiz;

    @Autowired
    UserAccountDao userAccountDao;

    @Autowired
    IOemProfitConfigServer iOemProfitConfigServer;

    public void profit(String jsonStr) throws Exception{
        try{
            Thread.sleep(2000);
        }catch (Exception e){

        }
        /**
         * 根据OEM不同的分润策略
         * 走不同的分分润
         */
        log.info("收到分润订单："+jsonStr);

        OrderInfo order = JSON.parseObject(jsonStr,OrderInfo.class);

        Long orderId = order.getId();


        OrderInfo orderInfo = this.orderInfoDao.getById(orderId);

        Integer status = orderInfo.getStatus();

        log.info("检测订单状态："+status);


        Integer profitSign = orderInfo.getProfitSign();

        log.info("检测订单分润状态："+profitSign);

        if(new Integer(1).equals(status)  && new Integer(0).equals(profitSign)){
            log.info("订单状态验证合法");
            Integer oemId = orderInfo.getOemId();
            OemProfitConfigInfo oemProfitConfigInfo =  this.iOemProfitConfigServer.getOemProfitConfig(oemId);

            String  profitStrategy = oemProfitConfigInfo.getProfitStrategy();
            log.info("检测oem商户分润模型 "+profitStrategy);
            if(ProfitStrategy.DEFAULT.equals(profitStrategy)){
                defaultProfitStrategy.profit(orderInfo);
            }else {
                percentageProfitStrategy.profit(orderInfo);
            }
        }else {
            log.info("订单未付款或者已经分润 本次分润终止");
        }




    }


}
